package com.deyuanyun.pic.pipe.controller;

import com.deyuanyun.pic.common.dto.PageJsonBean;
import com.deyuanyun.pic.common.util.ajax.AjaxSupport;
import com.deyuanyun.pic.pipe.controller.dto.PipeCrossQuery;
import com.deyuanyun.pic.pipe.controller.vo.PipeCrossVO;
import com.deyuanyun.pic.pipe.enumeration.PipeExportEnum;
import com.deyuanyun.pic.pipe.service.PipeMgrService;
import com.deyuanyun.pic.settings.util.DownExcelUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

/**
 * “穿跨越”控制类
 *
 * @作者: 严祥
 * @创建时间: 2015年8月18日
 * @类名称: PipeLineController.java
 */
@Controller
@RequestMapping("/pipe/pipeCross")
public class PipeCrossController extends PipeBaseController {

    @Resource
    private PipeMgrService pipeMgrService;

    /**
     * 跳转到 穿跨越 主页面
     *
     * @return
     */
    @RequestMapping("/toPipeCross")
    public ModelAndView toPipeCross() {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("basedata/pipe/pipeCross");
        return mv;
    }


    /**
     * 根据条件分页查询 穿跨越 数组对象
     *
     * @param pipeCrossQuery
     */
    @RequestMapping("/query.json")
    @ResponseBody
    public void query(PipeCrossQuery pipeCrossQuery) {
        PageJsonBean jsonBean = new PageJsonBean();
        jsonBean.setList(this.queryVO(pipeCrossQuery));
        AjaxSupport.sendSuccessText("success", jsonBean);
    }

    /**
     * 根据条件导出 穿跨越
     *
     * @param pipeCrossQuery
     * @throws IOException
     */
    @RequestMapping("/export.json")
    @ResponseBody
    public void export(PipeCrossQuery pipeCrossQuery) throws IOException {
        List list = this.queryVO(pipeCrossQuery);
        PipeExportEnum exportEnum = PipeExportEnum.PipeCross;
        String[] columnKeys = {"pipeLineName", "crossingNumber", "crossingType", "crossingName", "crossingTarget",
                "crossingMethod", "fromMileage", "fromReference", "fromReferenceDistance", "toMileage",
                "toReference", "toReferenceDistance", "locationDescription", "length", "number", "material",
                "wallThickness", "pipeStableMeasures", "isSleeve", "coatingMaterial", "coatingThickness", "completionDataNumber",
                "assemblyCompany", "inspectingCompany", "testingCompany", "recorder", "recordTime", "remark"};
        DownExcelUtil.export(exportEnum.getFileName(), pipePath+exportEnum.getTempPath(), exportEnum.getBeginRow(), list, columnKeys);
    }

    private List queryVO(PipeCrossQuery pipeCrossQuery) {
        return new PipeCrossVO().fromDomainList(pipeMgrService.queryPipeCross(pipeCrossQuery));
    }
}
